conventional GOSUB
GOSUB SubName calls subroutine SUB SubName, which executes until an EXIT SUB or END SUB
returns execution to the point following the call. Subroutines are more effective
than functions the more the following are true:
A large number of arguments would have to be passed to a function.
A routine uses many of the current function's AUTO variables.
A routine is needed only in the current function.
A routine is not very extensive.
Some routines are better implemented as subroutines than functions. Consider
converting a 20 line subroutine into a function. Many AUTO variables needed by the
subroutine would become SHARED so the new function could access them. SHARED
variables reduce encapsulation. A few variables could be passed as arguments, but
passing more than a few is awkward and time consuming. In contrast, subroutine
variables need not be passed or SHARED. All function variables are available to the
subroutine without overhead. Subroutines can result in more efficient, better structured
programs.
GOSUB example
Complex decision structures can be difficult to grasp. They stretch over many lines,
and executed lines are often scattered throughout the structure. A subroutine call
can replace several lines with a single name that describes the routine. The size of
the decision structure shrinks considerably, and descriptive routine names replace lines
of code whose purpose could not be understood at a glance.
SELECT CASE message$
CASE "CloseWindow" : GOSUB CloseWindow
CASE "DisplayWindow" : GOSUB DisplayWindow
CASE "ResizeWindow" : GOSUB ResizeWindow
CASE "HideWindow" : GOSUB HideWindow
CASE ELSE
: GOSUB
UnknownMessage
END SELECT
computed GOSUB
Computed GOSUB statements call subroutines whose addresses are in SUBADDR variables and
arrays.
GOSUB @subVar
GOSUB @subArray[i]
GOSUB @subVar calls the subroutine address in SUBADDR variable subVar. GOSUB @subArray[i] calls the subroutine address in element i of subArray[]. If subVar or subArray[i] is zero, no subroutine is called.
The SUBADDRESS() intrinsic returns subroutine addresses to be assigned to SUBADDR variables and arrays as follows:
subVar = SUBADDRESS (SubName)
subArray[i] = SUBADDRESS (SubName)
Computed GOSUB statements are especially efficient when one of a number of actions must be performed depending on some condition. The following computed GOSUB calls one of eight subroutines depending on a three bit field in token:
GOSUB @subAction[token{3,29}]